summaryrefslogtreecommitdiff
path: root/app/api/projects/[projectId]/cover/route.ts
blob: 802e2ab6896ec6d121aa9a980f7b10fcbd4465a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// app/api/projects/[projectId]/cover/route.ts
import { NextRequest, NextResponse } from "next/server"
import { generateCoverPage } from "@/lib/cover/cover-service"

/**
 * 커버페이지 생성 및 다운로드
 * 
 * GET /api/projects/[projectId]/cover?docNumber=DOC-001
 * 
 * @param projectId - 프로젝트 ID
 * @param docNumber - 문서 번호 (쿼리 파라미터)
 */
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ projectId: string }> }
) {
  try {
    const { projectId: projectIdParam } = await params
    const projectId = parseInt(projectIdParam)

    if (isNaN(projectId)) {
      return NextResponse.json(
        { success: false, message: "유효하지 않은 프로젝트 ID입니다" },
        { status: 400 }
      )
    }

    // docNumber 쿼리 파라미터 가져오기
    const { searchParams } = new URL(request.url)
    const docNumber = searchParams.get("docNumber")

    if (!docNumber) {
      return NextResponse.json(
        { success: false, message: "문서 번호(docNumber)가 필요합니다" },
        { status: 400 }
      )
    }

    console.log(`📄 커버페이지 요청 - ProjectID: ${projectId}, DocNumber: ${docNumber}`)

    // 커버페이지 생성
    const result = await generateCoverPage(projectId, docNumber)

    if (!result.success || !result.buffer) {
      return NextResponse.json(
        { 
          success: false, 
          message: result.error || "커버페이지 생성 실패" 
        },
        { status: 500 }
      )
    }

    // DOCX 파일로 응답
    // Buffer를 Uint8Array로 변환
    const uint8Array = new Uint8Array(result.buffer)
    
    return new NextResponse(uint8Array, {
      status: 200,
      headers: {
        "Content-Type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "Content-Disposition": `attachment; filename="${encodeURIComponent(result.fileName || "cover.docx")}"`,
        "Content-Length": result.buffer.length.toString(),
      },
    })

  } catch (error) {
    console.error("❌ 커버 페이지 생성 오류:", error)
    return NextResponse.json(
      { 
        success: false, 
        message: error instanceof Error ? error.message : "생성 중 오류 발생" 
      },
      { status: 500 }
    )
  }
}